# -*- coding: utf-8 -*-
import re

import requests

import cookie


class SetDispatch:
    ur='59.215.206.188'
    dispatch='ddglzy'
    cookie = cookie.cookie
    header={
        "Host": "{}".format(ur),
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0",
        "Accept": "application/json, text/javascript, */*; q=0.01",
        "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
        "Accept-Encoding": "gzip, deflate",
        "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
        "X-Requested-With": "XMLHttpRequest",
        "Content-Length": "311",
        "Origin": "http://{}".format(ur),
        "Connection": "keep-alive",
        #"Referer": "http://59.215.206.188/ddglzy/dispatch/resourse/itemconfigure/index",
        "Cookie": "{}".format(cookie).encode("utf-8"),
    }

    #查询调度系统
    def query(self,code,regCode):

        url='http://{}/{}/dispatch/resourse/itemconfigure/query'.format(self.ur,self.dispatch)
        data={
            "itemcode": "{}".format(code),
            "itemname": "",
            "regName": "",
            "regCode": "{}".format(regCode),
            # "itemname": "房地产开发项目前期物业服务合同备案",
            # "regName": "遵义市",
            # "regCode": "520300",
            "orgName": "",
            "orgCode": "",
            "type": "",
            "_search": "true",
            "nd": "",
            "rows": "10",
            "page": "1",
            "sidx": "sort",
            "sord": "asc",
        }
        x=requests.post(url,data=data,headers=self.header).json()
        print(x)
        #print(x)
        itemId=x['rows'][0]['cell']['ID']
        exchangeCode=itemCode=x['rows'][0]['cell']['CODE']
        organName=x['rows'][0]['cell']['ORG_NAME']
        organCode=x['rows'][0]['cell']['ORG_CODE']
        version=x['rows'][0]['cell']['VERSION']
        areaCode=x['rows'][0]['cell']['REGION_CODE']
        areaName=x['rows'][0]['cell']['REGION_NAME']
        itemTime=x['rows'][0]['cell']['AGREE_TIME']
        NAME=x['rows'][0]['cell']['NAME']
        #print(itemId,exchangeCode,itemCode,organName,organCode,version,areaCode,areaName,itemTime,NAME)
        return itemId,exchangeCode,itemCode,organName,organCode,version,areaCode,areaName,itemTime,NAME

    #获取事项ID
    def getID(self,code,regCode):
        head={
            "Host": "{}".format(self.ur),
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0",
            "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
            "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
            "Accept-Encoding": "gzip, deflate",
            "Connection": "keep-alive",
            "Referer": "http://{}/{}/dispatch/resourse/itemconfigure/index".format(self.ur,self.dispatch),
            "Cookie": "{}".format(self.cookie).encode("utf-8"),
            "Upgrade-Insecure-Requests": "1",
        }
        queryRe=SetDispatch().query(code,regCode)
        print(queryRe)

        url='http://{}/{}/dispatch/resourse/itemconfigure/itemshow?itemid={}&itemcode={}&version={}'.format(self.ur,self.dispatch,queryRe[0],queryRe[2],queryRe[5])

        x=requests.get(url,headers=head).text
        match =re.findall(r'value="(.*?)"\s+name="id"', x)
        #print(match)
        return match[0]
    #设置调度信息
    def updata(self,code,regCode):
        queryIdRe=SetDispatch().getID(code,regCode)
        queryRe=SetDispatch().query(code,regCode)

        url='http://{}/{}/dispatch/resourse/itemconfigure?method=save'.format(self.ur,self.dispatch)

        医保表单 = {"id": "{}".format(queryIdRe), "itemId": "{}".format(queryRe[0]),
                    "itemCode": "{}".format(queryRe[2]),
                    "areaCode": "{}".format(queryRe[6]), "areaName": "{}".format(queryRe[7]),
                    "itemTime": "{}".format(queryRe[8]),
                    "organName": "{}".format(queryRe[4]), "organCode": "{}".format(queryRe[5]),
                    "exchangeCode": "{}".format(queryRe[1]),
                    "sysInfo": [{"sysId": "A7875618DB3B46F1993D91BA606B582A", "sysType": "dispatch",
                                 "sysName": "产品调度系统3.5"},
                                {"sysType": "queuing", "sysName": ""},
                                {"sysType": "punlish", "sysName": ""},
                                {"sysId": "74AAC186781B42099DD1E0639A772E2F", "sysType": "online",
                                 "sysName": "网厅网上申报系统"},
                                {"sysType": "hallManager", "sysName": ""},
                                {"sysId": "B1C13E3EC16D477C991E6ECB4D2F5747", "sysType": "accept",
                                 "sysName": "业务中台"},
                                {"sysId": "", "sysType": "approve", "sysName": ""}
                                ]}





        贵州省医疗保障信息系统二类事项=[
                        '门诊费用报销-门诊慢特病',
                        '住院费用报销',
                        '产前检查费支付',
                        '生育医疗费支付',
                        '计划生育医疗费支付',
                        '医疗救助对象手工（零星）报销',
                        '门诊费用报销-普通门诊'
                    ]
        地就医备案变更=['异地安置退休人员备案-变更',
                    '异地长期居住人员备案-变更',
                    '常驻异地工作人员备案-变更',
                    '异地转诊人员备案-变更',
                    '其他临时外出就医人员备案-变更']
        地就医备案撤销=['异地安置退休人员备案-取消',
                    '异地长期居住人员备案-取消',
                    '常驻异地工作人员备案-取消',
                    '异地转诊人员备案-取消',
                    '其他临时外出就医人员备案-取消']
        地就医备案登记=['异地安置退休人员备案-登记',
                    '异地长期居住人员备案-登记',
                    '常驻异地工作人员备案-登记',
                    '异地转诊人员备案-登记',
                    '其他临时外出就医人员备案-登记']
        账户共济登记接口=['职工医保个人账户家庭共济办理']

        print("queryRe[8]"+queryRe[9])
        if (queryRe[9] in 账户共济登记接口):
            医保表单['sysInfo'][6] = {
                "sysId": "F04B6B8D405443D5878A72AE16341010",
                "sysType": "approve",
                "sysName": "账户共济登记接口"
            }
        if (queryRe[9] in 贵州省医疗保障信息系统二类事项):
            医保表单['sysInfo'][6] = {
		"sysId": "893BB2D3393148D0B425C1084A26BA7F",
		"sysType": "approve",
		"sysName": "贵州省医疗保障信息系统（二类事项）"
	}
        if (queryRe[9] in 地就医备案变更):
            医保表单['sysInfo'][6] = {
		"sysId": "582A1C9D775242599C6F6F24F854829C",
		"sysType": "approve",
		"sysName": "异地就医备案-变更（就医报销一件事）"
	}
        if (queryRe[9] in 地就医备案撤销):
            医保表单['sysInfo'][6] = {
		"sysId": "E67FE8A6868548FBB834054001E18CCE",
		"sysType": "approve",
		"sysName": "异地就医备案-撤销（就医报销一件事）"
	}
        if (queryRe[9] in 地就医备案登记):
            医保表单['sysInfo'][6] = {
		"sysId": "EAE5C8B63D0D4147BD3F73FFE2860B73",
		"sysType": "approve",
		"sysName": "异地就医备案-登记（就医报销一件事）"
	}
    # else:print(queryRe[8]+"事项不存在,请检查")

        x=requests.post(url,json=医保表单,headers=self.header).json()
        print("医保表单："+str(医保表单))
        print(queryRe[7]+">"+queryRe[-1]+":"+str(x['statusCode']))
    #按区划循环
    def loop(self):
        org = {

            "遵义市": "520300",
            "红花岗区": "520302",
            "汇川区": "520303",
            "播州区": "520321",
            "桐梓县": "520322",
            "绥阳县": "520323",
            "正安县": "520324",
            "道真县": "520325",
            "务川县": "520326",
            "凤冈县": "520327",
            "湄潭县": "520328",
            "余庆县": "520329",
            "习水县": "520330",
            "赤水市": "520381",
            "仁怀市": "520382",
            "新蒲新区": "520383",



        }
        from 医保报销一件事获取虚拟事项 import 获取虚拟事项

        for org,code in org.items():



            for i in 获取虚拟事项().start(regionCode=code):


                SetDispatch().updata(i,code)


#SetDispatch().query('00A36A4247524DCFBCE6FAD3A177E42E','520302')
#SetDispatch().updata('00A36A4247524DCFBCE6FAD3A177E42E','520302')
#SetDispatch().getID('00A36A4247524DCFBCE6FAD3A177E42E','520302')
SetDispatch().loop()
